/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.yczbj.ycvideoplayer.ability;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import com.yc.video.bridge.ControlWrapper;
import com.yc.video.config.ConstantKeys;
import com.yc.video.player.SimpleStateListener;
import com.yc.video.player.VideoPlayer;
import com.yc.video.ui.view.BasisVideoController;

import org.yczbj.ycvideoplayer.ConstantVideo;
import org.yczbj.ycvideoplayer.ResourceTable;
import org.yczbj.ycvideoplayer.utils.ImageLoader;
import org.yczbj.ycvideoplayer.view.MyDanmakuView;

/**
 * 7.1 播放器添加视频自定义弹幕视图
 *
 * @since 2021-05-10
 */
public class DanmuAbility extends BaseAbility implements Component.ClickedListener {
    private VideoPlayer mVideoPlayer;
    private Button mBtnShow;
    private Button mBtnHide;
    private Button mBtnAddDan;
    private Button mBtnAddCustomDan;

    private MyDanmakuView mMyDanmakuView;

    private final int delayTime = 100;
    private EventHandler mHandler = new EventHandler(EventRunner.getMainEventRunner());

    @Override
    protected void onActive() {
        super.onActive();
        if (mVideoPlayer != null) {
            mVideoPlayer.resume();
        }
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        if (mVideoPlayer != null) {
            mVideoPlayer.pause();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mVideoPlayer != null) {
            mVideoPlayer.release();
        }
        if (mHandler != null) {
            mHandler.removeAllEvent();
            mHandler = null;
        }
    }

    @Override
    public void onBackPressed() {
        ControlWrapper.defaultValue();
        if (mVideoPlayer == null || !mVideoPlayer.onBackPressed()) {
            super.onBackPressed();
        }
    }

    @Override
    protected int getLayoutResId() {
        return ResourceTable.Layout_ability_danmu_player;
    }

    @Override
    protected void initComponent(Intent intent) {
        super.initComponent(intent);
        mVideoPlayer = (VideoPlayer) findComponentById(ResourceTable.Id_video_player);
        mBtnShow = (Button) findComponentById(ResourceTable.Id_btn_show);
        mBtnHide = (Button) findComponentById(ResourceTable.Id_btn_hide);
        mBtnAddDan = (Button) findComponentById(ResourceTable.Id_btn_add_dan);
        mBtnAddCustomDan = (Button) findComponentById(ResourceTable.Id_btn_add_custom_dan);
        mBtnAddCustomDan = (Button) findComponentById(ResourceTable.Id_btn_add_custom_dan);
        mMyDanmakuView = new MyDanmakuView(this);
        mMyDanmakuView.addDrawTask(null);
        mMyDanmakuView.setLayoutRefreshedListener(null);

        mController = new BasisVideoController(this);
        mController.addControlComponent(mMyDanmakuView);

        // 设置视频背景图
        ImageLoader.loadImage(getContext(), ResourceTable.Media_image_default, mController.getThumb());
        // 设置控制器
        mVideoPlayer.setController(mController);
        mVideoPlayer.setUrl(ConstantVideo.VideoPlayerList.get(0));
        mVideoPlayer.setScreenScaleType(ConstantKeys.PlayerScreenScaleType.SCREEN_SCALE_16_9);
        mVideoPlayer.start();
        mVideoPlayer.addOnStateChangeListener(new SimpleStateListener() {
            @Override
            public void onPlayStateChanged(int playState) {
                if (playState == ConstantKeys.CurrentState.STATE_PREPARED) {
                    simulateDanmu();
                } else if (playState == ConstantKeys.CurrentState.STATE_BUFFERING_PLAYING) {
                    mHandler.removeAllEvent();
                }
            }
        });
    }

    @Override
    protected void initListener() {
        super.initListener();
        mBtnShow.setClickedListener(this);
        mBtnHide.setClickedListener(this);
        mBtnAddDan.setClickedListener(this);
        mBtnAddCustomDan.setClickedListener(this);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_btn_show:
                mMyDanmakuView.show();
                break;
            case ResourceTable.Id_btn_hide:
                mMyDanmakuView.hide();
                break;
            case ResourceTable.Id_btn_add_dan:
                mMyDanmakuView.addDanmakuWithDrawable();
                break;
            case ResourceTable.Id_btn_add_custom_dan:
                mMyDanmakuView.addDanmaku("小杨逗比自定义弹幕~", true);
                break;
            default:
                break;
        }
    }

    /**
     * 模拟弹幕
     */
    private void simulateDanmu() {
        mHandler.postTask(new Runnable() {
            @Override
            public void run() {
                mMyDanmakuView.addDanmaku("awsl", false);
                mHandler.postTask(this, delayTime);
            }
        });
    }
}
